#include "GUICfgComputationSettingWidget.h"
#include "ui_GUICfgComputationSettingWidget.h"

#include "FITK_Interface/FITKInterfaceCustomData/FITKCDInterface.h"
#include "FITK_Interface/FITKInterfaceCustomData/FITKCDCfgCSetComputation.h"
#include "FITK_Interface/FITKInterfaceCustomData/FITKCDCfgCSetBase.h"
#include "FITK_Interface/FITKInterfaceCustomData/FITKCDCfgCSetPath.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "OperatorsInterface/TreeEventOperator.h"

using namespace GUI;

GUICfgComputationSettingWidget::GUICfgComputationSettingWidget(EventOper::ParaWidgetInterfaceOperator* oper, QWidget* parent) :
	GUIWidgetBase(parent), _oper(oper)
{
	_ui = new Ui::GUICfgComputationSettingWidget();
	_ui->setupUi(this);

	init();
}

GUICfgComputationSettingWidget::~GUICfgComputationSettingWidget()
{
	if (_ui) {
		delete _ui;
		_ui = nullptr;
	}
}

void GUICfgComputationSettingWidget::init()
{
	auto pComputationManager = Interface::FITKCDInterface::getInstance()->getCDCfgComputationManager();
	_pComputation = pComputationManager->getDataByName("Computation");
	if (_pComputation == nullptr)
	{
		_pComputation = new Interface::FITKCDCfgCSetComputation();
		_pComputation->setDataObjectName("Computation");

		pComputationManager->appendDataObj(_pComputation);
	}

	auto pBaseManager = Interface::FITKCDInterface::getInstance()->getCDCfgBaseManager();
	_pBase = pBaseManager->getDataByName("Base");
	if (_pBase == nullptr)
	{
		_pBase = new Interface::FITKCDCfgCSetBase();
		_pBase->setDataObjectName("Base");

		pBaseManager->appendDataObj(_pBase);
	}

	auto pPathManager = Interface::FITKCDInterface::getInstance()->getCDCfgPathManager();
	_pPath = pPathManager->getDataByName("Path");
	if (_pPath == nullptr)
	{
		_pPath = new Interface::FITKCDCfgCSetPath();
		_pPath->setDataObjectName("Path");

		pPathManager->appendDataObj(_pPath);
	}

	_ui->comboBox_Smoothed_Kernel_Function->addItems(QStringList() << "Default" << "Cubic Spline" << "Gaussian Kernel" << "Quintic Spline" << "Wendland C2");
	_ui->comboBox_Time_Integration->addItems(QStringList() << "Leap Frog" << "Predict" << "Correct");
	_ui->comboBox_Output_File_Type->addItems(QStringList() << "Tecplot" << "Paraview vtk" << "Paraview vtu");

	setDataToWidget();
}

void GUICfgComputationSettingWidget::setDataToWidget()
{
	_ui->doubleSpinBox_Minimum_xyz0->setValue(_pComputation->getMinimum_xyz()[0]);
	_ui->doubleSpinBox_Minimum_xyz1->setValue(_pComputation->getMinimum_xyz()[1]);
	_ui->doubleSpinBox_Minimum_xyz2->setValue(_pComputation->getMinimum_xyz()[2]);
	_ui->doubleSpinBox_Maximum_xyz0->setValue(_pComputation->getMaximum_xyz()[0]);
	_ui->doubleSpinBox_Maximum_xyz1->setValue(_pComputation->getMaximum_xyz()[1]);
	_ui->doubleSpinBox_Maximum_xyz2->setValue(_pComputation->getMaximum_xyz()[2]);

	_ui->doubleSpinBox_Sound_Speed->setValue(_pBase->getSound_Speed());
	_ui->doubleSpinBox_CFL->setValue(_pBase->getCFL());
	_ui->doubleSpinBox_Background_Pressure->setValue(_pBase->getBackground_Pressure());
	_ui->doubleSpinBox_Initial_dx->setValue(_pBase->getInitial_dx());
	_ui->doubleSpinBox_Simulation_Time->setValue(_pBase->getSimulation_Time());
	_ui->doubleSpinBox_Save_Time_Steps->setValue(_pBase->getSave_Time_Steps());
	_ui->doubleSpinBox_Monitor_Time_Steps->setValue(_pBase->getMonitor_Time_Steps());
	_ui->spinBox_Print_Steps->setValue(_pBase->getPrint_Steps());
	_ui->spinBox_Maximum_Particles->setValue(_pBase->getMaximum_Particles());
	_ui->comboBox_Time_Integration->setCurrentIndex(_pBase->getTime_Integration());
	_ui->comboBox_Output_File_Type->setCurrentIndex(_pBase->getOutput_File_Type());
	_ui->checkBox_Bound_Output->setChecked(_pBase->getBound_Output());

	_ui->lineEdit_Data_Storage_Path->setText(_pPath->getData_Storage_Path());
	_ui->lineEdit_Data_Reading_Path->setText(_pPath->getData_Reading_Path());

	_ui->spinBox_Compute_Force->setValue(_pBase->getCompute_Force());
	_ui->checkBox_Compute_Rigid_Normalvector->setChecked(_pBase->getCompute_Rigid_Normalvector());
	_ui->checkBox_Delete_Particles->setChecked(_pBase->getDelete_Particles());
	_ui->checkBox_Rigid_Output->setChecked(_pBase->getRigid_Output());
	_ui->lineEdit_Name_Of_STL->setText(_pPath->getName_Of_STL());
}


void GUICfgComputationSettingWidget::on_doubleSpinBox_Minimum_xyz0_valueChanged(double value)
{
	auto d = _pComputation->getMinimum_xyz();
	d[0] = value;
	_pComputation->setMinimum_xyz(d);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Minimum_xyz1_valueChanged(double value)
{
	auto d = _pComputation->getMinimum_xyz();
	d[1] = value;
	_pComputation->setMinimum_xyz(d);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Minimum_xyz2_valueChanged(double value)
{
	auto d = _pComputation->getMinimum_xyz();
	d[2] = value;
	_pComputation->setMinimum_xyz(d);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Maximum_xyz0_valueChanged(double value)
{
	auto d = _pComputation->getMaximum_xyz();
	d[0] = value;
	_pComputation->setMaximum_xyz(d);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Maximum_xyz1_valueChanged(double value)
{
	auto d = _pComputation->getMaximum_xyz();
	d[1] = value;
	_pComputation->setMaximum_xyz(d);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Maximum_xyz2_valueChanged(double value)
{
	auto d = _pComputation->getMaximum_xyz();
	d[2] = value;
	_pComputation->setMaximum_xyz(d);
}


void GUICfgComputationSettingWidget::on_doubleSpinBox_Sound_Speed_valueChanged(double value)
{
	_pBase->setSound_Speed(value);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_CFL_valueChanged(double value)
{
	_pBase->setCFL(value);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Background_Pressure_valueChanged(double value)
{
	_pBase->setBackground_Pressure(value);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Initial_dx_valueChanged(double value)
{
	_pBase->setInitial_dx(value);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Simulation_Time_valueChanged(double value)
{
	_pBase->setSimulation_Time(value);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Save_Time_Steps_valueChanged(double value)
{
	_pBase->setSave_Time_Steps(value);
}
void GUICfgComputationSettingWidget::on_doubleSpinBox_Monitor_Time_Steps_valueChanged(double value)
{
	_pBase->setMonitor_Time_Steps(value);
}
void GUICfgComputationSettingWidget::on_spinBox_Print_Steps_valueChanged(int value)
{
	_pBase->setPrint_Steps(value);
}
void GUICfgComputationSettingWidget::on_spinBox_Maximum_Particles_valueChanged(int value)
{
	_pBase->setMaximum_Particles(value);
}
void GUICfgComputationSettingWidget::on_comboBox_Smoothed_Kernel_Function_activated(int index)
{
	_pBase->setSmoothed_Kernel_Function(index);
}
void GUICfgComputationSettingWidget::on_comboBox_Time_Integration_activated(int index)
{
	_pBase->setTime_Integration(index);
}
void GUICfgComputationSettingWidget::on_comboBox_Output_File_Type_activated(int index)
{
	_pBase->setOutput_File_Type(index);
}
void GUICfgComputationSettingWidget::on_checkBox_Bound_Output_stateChanged(int state)
{
	_pBase->setBound_Output(state);
}


void GUICfgComputationSettingWidget::on_lineEdit_Data_Storage_Path_editingFinished()
{
	_pPath->setData_Storage_Path(_ui->lineEdit_Data_Storage_Path->text());
}
void GUICfgComputationSettingWidget::on_lineEdit_Data_Reading_Path_editingFinished()
{
	_pPath->setData_Reading_Path(_ui->lineEdit_Data_Reading_Path->text());
}


void GUICfgComputationSettingWidget::on_spinBox_Compute_Force_valueChanged(int value)
{
	_pBase->setCompute_Force(value);
}
void GUICfgComputationSettingWidget::on_checkBox_Compute_Rigid_Normalvector_stateChanged(int state)
{
	_pBase->setCompute_Rigid_Normalvector(state);
}
void GUICfgComputationSettingWidget::on_checkBox_Delete_Particles_stateChanged(int state)
{
	_pBase->setDelete_Particles(state);
}
void GUICfgComputationSettingWidget::on_checkBox_Rigid_Output_stateChanged(int state)
{
	_pBase->setRigid_Output(state);
}
void GUICfgComputationSettingWidget::on_lineEdit_Name_Of_STL_editingFinished()
{
	_pPath->setName_Of_STL(_ui->lineEdit_Name_Of_STL->text());
}

